package qualomelhor.game;

import static playn.core.PlayN.assets;

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

import playn.core.Image;
import qualomelhor.game.entities.NewDynamicPhysicsEntity;

public class Mao2 extends NewDynamicPhysicsEntity {
	
	public static String TYPE = "Mao";
	private static Image image = assets().getImage("/images/avatar/mao/mao1.png");
	//private static Image image = loadImage("/images/pontodir.png");

	private Image imgMao;
	private int maoId;
	private float width;
	private float height;
	private double hipMedio;
	private double angleBeta;
	private double angleRotacao;
	private int posX;
	private int posY;
	private double posXL;
	private double posYL;
	private double posXLAbs;
	private double posYLAbs;
	private float physUnitPerScreenUnit;

	public Mao2 (MyWorld myWorld, World world, float x, float y, float w, float h, float angle, int sfx, int sfy, Image imgMao, float physUnitPerScreenUnit) {
		super(myWorld, world, x, y, (float)(Math.round(w*100.0)/100.0), (float)(Math.round(h*100.0)/100.0), angle, sfx, sfy, imgMao);
		this.imgMao = imgMao;
		this.physUnitPerScreenUnit = physUnitPerScreenUnit;
		this.width = (float)(Math.round(w*100.0)/100.0);
		this.height = (float)(Math.round(h*100.0)/100.0);
		TesteGame.setMessageBox("w,h (corrigidas em Mao.java) = " + this.width + "," + this.height);
		//hipMedio = Math.sqrt(Math.pow(width, 2) + Math.pow(height / 2, 2));
		//angleBeta = Math.atan(((double)height / 2) / ((double)width));
		
		//setImage(imgMao);
	}

	public Body initPhysicsBody(World world, float x, float y, float angle) {
		FixtureDef fixtureDef = new FixtureDef();
		BodyDef bodyDef = new BodyDef();
		bodyDef.type = BodyType.DYNAMIC;
		bodyDef.position = new Vec2(x, y);
		Body body = world.createBody(bodyDef);

		PolygonShape pShape = new PolygonShape();
		pShape.setAsBox(getWidth() / 2, getHeight() / 2, new Vec2(getWidth() / 2, getHeight() / 2), 0f);

		fixtureDef.shape = pShape;
		fixtureDef.density = 5.4f;
		fixtureDef.friction = 0.1f;
		body.createFixture(fixtureDef);

		return body;
	}

	float getRadius() {
		//return getWidth() / 2;
    	return 3.50f;
		//return 5 * width;
		//return width / 2;
	}

	//public float getWidth() {
		//return imgMao.width();
		//return 5.0f;
		//return 2.84f;
		//return width;
	//}

	//public float getHeight() {
		//return imgMao.height();
		//return 5.0f;
		//return 3.74f;
		//return height;
	//}

	public int getMaoId() {
		return maoId;
	}

	public Image getImage() {
		return imgMao;
		//return image;
	}

	public double getHidMedio() {
		return hipMedio;
	}

	public double getAngleBeta() {
		return angleBeta;
	}

	public double getAngleRotacao() {
		return angleRotacao;
	}

	public int getPosX() {
		return posX;
	}

	public int getPosY() {
		return posY;
	}

	public double getPosXL() {
		return posXL;
	}

	public double getPosYL() {
		return posYL;
	}

	public double getPosXLAbs() {
		return posXLAbs;
	}

	public double getPosYLAbs() {
		return posYLAbs;
	}

	public void setImage(Image imgMao) {
		this.imgMao = imgMao;
		width = (int)imgMao.width();
		height = (int)imgMao.height();
		hipMedio = Math.sqrt(Math.pow(width, 2) + Math.pow(height / 2, 2));
		angleBeta = Math.atan(((double)height / 2) / ((double)width));
	}

	public void setMaoId(int maoId) {
		this.maoId = maoId;
	}

	public void setAngleRotation(double angle) {
		this.angleRotacao = angle;
		posXL = hipMedio * Math.cos(angleBeta + angleRotacao);
		posYL = hipMedio * Math.sin(angleBeta + angleRotacao);
		posXLAbs = hipMedio * Math.cos(angleBeta + Math.abs(angleRotacao));
		posYLAbs = hipMedio * Math.sin(angleBeta + Math.abs(angleRotacao));
	}
	
}